package com.zuipin.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zuipin.entity.SysMenu;
import com.zuipin.entity.SysResource;
import com.zuipin.framework.annotation.Inject;
import com.zuipin.framework.annotation.Valid;
import com.zuipin.framework.annotation.Valids;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.result.PageResult;
import com.zuipin.framework.result.Result;
import com.zuipin.service.ISysMenuService;
import com.zuipin.service.ISysResourceService;
import com.zuipin.util.Pagination;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 资源(按纽/功能)Controller
 * 
 * @ClassName: SysResourceController
 * @Description: TODO
 * @author robot
 * @date 2017年09月147日 09:21:09
 */
@Api(value = "资源(按纽/功能)", description = "资源(按纽/功能)")
@RestController
@RequestMapping(value = "/sysResource")
public class SysResourceController {
	@Resource
	public ISysResourceService	sysResourceService;
	@Resource
	private ISysMenuService		sysMenuService;
	
	@RequestMapping(value = "/get", method = RequestMethod.GET)
	@ApiOperation(value = "根据主键查询资源(按纽/功能)", notes = "查询资源(按纽/功能)")
	public Result<SysResource> getSysResourceById(@ApiParam(required = true, name = "id", value = "主键 自增长") @RequestParam Long id) {
		SysResource sysResource = sysResourceService.findById(id);
		Result<SysResource> result = new Result<SysResource>();
		if (sysResource != null) {
			SysMenu sysMenu = sysMenuService.findById(sysResource.getMenuId());
			if (sysMenu != null) {
				sysResource.setMenuName(sysMenu.getMenuName());
			}
			result.setData(sysResource);
			result.setErrorCode(ErrorCode.SUCCESS);
			result.setMessage("查询成功");
		} else {
			result.setErrorCode(ErrorCode.ELSE);
		}
		return result;
	}
	
	@RequestMapping(value = "/getSysResourceByCode", method = RequestMethod.GET)
	@ApiOperation(value = "6期-根据Code查询资源(按纽/功能)-zhouhuihang", notes = "Code资源(按纽/功能)")
	public Result<SysResource> getSysResourceByCode(@ApiParam(required = true, name = "sysId", value = "系统Id") @RequestParam Long sysId,
			@ApiParam(required = true, name = "resourceCode", value = "资源编号") @RequestParam String resourceCode) {
		SysResource sysResource = sysResourceService.findByCode(resourceCode, sysId);
		Result<SysResource> result = new Result<SysResource>();
		if (sysResource != null) {
			SysMenu sysMenu = sysMenuService.findById(sysResource.getMenuId());
			if (sysMenu != null) {
				sysResource.setMenuName(sysMenu.getMenuName());
			}
			result.setData(sysResource);
			result.setErrorCode(ErrorCode.SUCCESS);
			result.setMessage("查询成功");
		} else {
			result.setErrorCode(ErrorCode.ELSE);
		}
		return result;
	}
	
	@RequestMapping(value = "/del", method = RequestMethod.DELETE)
	@ApiOperation(value = "根据主键删除资源(按纽/功能)", notes = "删除资源(按纽/功能)")
	public Result<SysResource> delSysResource(Long id) {
		Result<SysResource> result = new Result<SysResource>();
		sysResourceService.delete(id);
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@RequestMapping(value = "/addOrUpdateSysResource", method = RequestMethod.POST)
	@ApiOperation(value = "6期-zhouhuihang-新增/修改资源(按纽/功能)", notes = "新增/修改资源(按纽/功能)")
	@Valids({ @Valid(name = "sysResource.sysId", maxLength = 19), //
			@Valid(name = "sysResource.resourceName", maxLength = 100), //
			@Valid(name = "sysResource.menuId", maxLength = 19), //
			@Valid(name = "sysResource.resourceCode", maxLength = 100), //
			@Valid(name = "sysResource.hrefUrl", maxLength = 200), //
			@Valid(name = "sysResource.sort", maxLength = 10), //
			@Valid(name = "sysResource.resourceStatus", maxLength = 3, regex = "0|1"), //
			@Valid(name = "sysResource.remark", maxLength = 255), //
			@Valid(name = "sysResource.isPublic", maxLength = 3, regex = "0|1"), //
			@Valid(name = "sysResource.createId", maxLength = 10), //
			@Valid(name = "sysResource.createTime", maxLength = 20), //
			@Valid(name = "sysResource.updateId", maxLength = 10), //
			@Valid(name = "sysResource.updateTime", maxLength = 20), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "sysResource.id", value = "主键 自增长", required = false, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "sysResource.sysId", value = "系统id", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "sysResource.resourceName", value = "资源名称", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "sysResource.menuId", value = "模块(菜单)id", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "sysResource.resourceCode", value = "资源编号", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "sysResource.hrefUrl", value = "资源(按纽/功能)链接", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "sysResource.sort", value = "排序号", required = false, dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "sysResource.remark", value = "模块描述,/备注", required = false, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "sysResource.isPublic", value = "是否为公共模块（任务角色都能拥有的权限）", required = true, dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "sysResource.createId", value = "操作人", dataType = "long", required = false, paramType = "query"),
			@ApiImplicitParam(name = "sysResource.updateId", value = "更新人", dataType = "long", required = false, paramType = "query"), })
	public Result<SysResource> addOrUpdateSysResource(@Inject @ApiIgnore SysResource sysResource) {
		if (sysResource.getId() == null) {
			return sysResourceService.insertBack(sysResource);
		} else {
			return sysResourceService.updateBack(sysResource);
			
		}
	}
	
	@RequestMapping(value = "/seach", method = RequestMethod.POST)
	@ApiOperation(value = "6期-zhouhuihang-搜索资源(按纽/功能)SKU资源(按纽/功能)并分页", notes = "搜索资源(按纽/功能)并分页")
	@Valids({ @Valid(name = "sysResource.sysId", maxLength = 19), @Valid(name = "sysResource.resourceName", maxLength = 100), @Valid(name = "sysResource.menuId", maxLength = 19),
			@Valid(name = "sysResource.resourceCode", maxLength = 100), @Valid(name = "sysResource.hrefUrl", maxLength = 200), @Valid(name = "sysResource.sort", maxLength = 10),
			@Valid(name = "sysResource.delFlag", maxLength = 3), @Valid(name = "sysResource.resourceStatus", maxLength = 3), @Valid(name = "sysResource.remark", maxLength = 255),
			@Valid(name = "sysResource.isPublic", maxLength = 3), @Valid(name = "sysResource.createId", maxLength = 10), @Valid(name = "sysResource.createTime", maxLength = 20),
			@Valid(name = "sysResource.updateId", maxLength = 10), @Valid(name = "sysResource.updateTime", maxLength = 20), @Valid(name = "page.pageNumber", maxValue = 999),
			@Valid(name = "page.pageSize", maxValue = 999), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "sysResource.sysId", value = "系统id", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "sysResource.resourceName", value = "资源名称", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "sysResource.menuId", value = "模块(菜单)id", dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "sysResource.resourceCode", value = "资源编号", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "sysResource.hrefUrl", value = "资源(按纽/功能)链接", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "sysResource.sort", value = "排序号", dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "sysResource.delFlag", value = "0:未删除 1:已删除", dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "sysResource.resourceStatus", value = "是否冻结 0:未冰结 1已冻结", dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "sysResource.remark", value = "模块描述,/备注", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "sysResource.isPublic", value = "是否为公共模块（任务角色都能拥有的权限）", dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "sysResource.createId", value = "操作人", dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "sysResource.createTime", value = "创建时间", required = false, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "sysResource.updateId", value = "更新人", dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "sysResource.updateTime", value = "更新时间", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "page.pageNumber", value = "页码", required = true, dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "page.pageSize", value = "单页记录数", required = true, dataType = "integer", paramType = "query"), })
	public Result<List<SysResource>> seachSysResource(@Inject @ApiIgnore SysResource sysResource, @Inject @ApiIgnore Pagination page) {
		PageResult<List<SysResource>> result = new PageResult<List<SysResource>>();
		List<SysResource> data = sysResourceService.seachSysResource(sysResource, page);
		for (SysResource sysRes : data) {
			SysMenu sysMenu = sysMenuService.findById(sysRes.getMenuId());
			if (sysMenu != null) {
				sysRes.setMenuName(sysMenu.getMenuName());
			}
		}
		result.setData(data);
		result.setTotal_record(page.getTotalRecord());
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	/**
	 * @date  2017-9-18 14:41:31
	 * @author zhouhuihang
	 * @param id
	 * @param status
	 * @return
	 */
	@RequestMapping(value = "/modifyResourceStatus", method = RequestMethod.POST)
	@ApiOperation(value = "6期-zhouhuihang-更改资源(按纽/功能)状态", notes = "更改资源(按纽/功能)状态")
	@ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "主键ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "status", value = "0|1   0启用 1禁用", dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "updateId", value = "修改人Id", required = true, dataType = "integer", paramType = "query"), })
	public Result modifyResourceStatus(@RequestParam(required = true) Long id, @RequestParam(required = true) Integer status, @RequestParam(required = false) Integer updateId) {
		Result result = new Result();
		SysResource sysResource = sysResourceService.findById(id);
		if (sysResource == null) {
			result.setErrorCode(ErrorCode.NOT_FIND);
			result.setMessage("查询不到资源");
			return result;
		}
		String msg = "";
		if (sysResource.getResourceStatus().equals(SysResource.ENABLE)) {
			msg = "启用";
		} else {
			msg = "禁用";
		}
		if (sysResource.getResourceStatus().equals(status)) {
			result.setErrorCode(ErrorCode.MODITY_STATUS_FAIL);
			result.setMessage("状态已经" + msg + "不能重复" + msg);
			return result;
		}
		SysResource modifyResource = new SysResource();
		modifyResource.setId(id);
		modifyResource.setResourceStatus(status);
		
		modifyResource.setUpdateId(updateId);
		sysResourceService.update(modifyResource);
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setMessage("资源(按纽/功能)状态已改变成功");
		return result;
	}
	
	/**
	 * @date  2017-9-18 14:41:31
	 * @author zhouhuihang
	 * @param id
	 * @param status
	 * @return
	 */
	@RequestMapping(value = "/setResourcePublicStatus", method = RequestMethod.POST)
	@ApiOperation(value = "6期-zhouhuihang-设置资源(按纽/功能)是否为公共模块", notes = "设置资源(按纽/功能)是否为公共模块")
	@ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "主键ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "isPublic", value = "0|1  0非公共 1公共 ", dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "updateId", value = "修改人Id", required = true, dataType = "integer", paramType = "query"), })
	public Result setResourcePublicStatus(@RequestParam(required = true) Long id, @RequestParam(required = true) Integer isPublic,
			@RequestParam(required = false) Integer updateId) {
		Result result = new Result();
		SysResource sysResource = sysResourceService.findById(id);
		if (sysResource == null) {
			result.setErrorCode(ErrorCode.NOT_FIND);
			result.setMessage("查询不到资源");
			return result;
		}
		String msg = "";
		if (sysResource.getIsPublic().equals(SysResource.IS_PUBLIC)) {
			msg = "公开";
		} else {
			msg = "私有";
		}
		if (sysResource.getIsPublic().equals(isPublic)) {
			result.setErrorCode(ErrorCode.MODITY_STATUS_FAIL);
			result.setMessage("资源(按纽/功能)已为" + msg + "状态,不能重复" + msg);
			return result;
		}
		SysResource modifyResource = new SysResource();
		modifyResource.setId(id);
		modifyResource.setIsPublic(isPublic);
		modifyResource.setUpdateId(updateId);
		sysResourceService.update(modifyResource);
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setMessage("设置成功");
		return result;
	}
	
}
